/**
 * [getCalendarMonth 获得指定日期所在月的第一周到第四/五周的数据组合，形如：
 * [{
    "date": "2016/10/30", //日期字符串
    "dateNum": 30,  //日
    "isCurMonth": false, //是否当前月
    "weekIndex": 0 //是本月的第几周，下标从0开始
  },{
    "date": "2016/10/31",
    "dateNum": 31,
    "isCurMonth": false,
    "weekIndex": 0
  },{
    "date": "2016/11/1",
    "dateNum": 1,
    "day": 2,
    "isCurMonth": true,
    "isToday": false,
    "weekIndex": 0
  }]
  ]
 * @param  {[Date]} param [指定的日期，非必填，默认为当天]
 * @return {[Array]}       [获得指定日期所在月的第一周到第四/五周的数据组合]
 */
export function getCalendarMonth(date) {
    date = date ? date : new Date();
    let y = date.getFullYear();
    let m = date.getMonth();
    let _m;
    let firstDay = new Date(y, m, 1).getDay(); //当月第一天 周期
    let days = monthSize(date); //当月天数
    let prevMonthDays = monthSize(new Date(y, m - 1)); //上月天数
    let initPrevDay = prevMonthDays - firstDay;
    let lines = Math.ceil((firstDay + days) / 7);
    _m = new Array(lines * 7);
    let nextMonthDay = 0;

    for (let i = 0; i < _m.length; i++) {
        let weekIndex = parseInt(i / 7);
        if (i < firstDay) {
            let date = ++initPrevDay;
            if (m === 0 && date > 7) {
                _m[i] = {
                    isCurMonth: false,
                    dateNum: date,
                    weekIndex,
                    date: `${y - 1}/${12}/${date}`
                };
            } else {
                _m[i] = {
                    isCurMonth: false,
                    dateNum: date,
                    weekIndex,
                    date: `${y}/${m}/${date}`
                };
            }
        } else if (i >= (firstDay + days)) {
            let date = ++nextMonthDay;

            if (m === 11 && date <= 7) {
                _m[i] = {
                    isCurMonth: false,
                    dateNum: date,
                    weekIndex,
                    date: `${y + 1}/${1}/${date}`
                };
            } else {
                _m[i] = {
                    isCurMonth: false,
                    dateNum: date,
                    weekIndex,
                    date: `${y}/${m + 2}/${date}`
                };
            }
        } else {
            let _date = i - firstDay + 1;
            let today = new Date();
            let today_y = today.getFullYear();
            let today_m = today.getMonth();
            let today_d = today.getDate();
            let isToday = today_y === y && today_m === m && today_d === _date ? true : false;
            _m[i] = {
                dateNum: _date, //日期
                day: i % 7, //周期
                weekIndex,
                isCurMonth: true,
                isToday,
                date: `${y}/${m + 1}/${_date}`
            };
        }
    }
    return _m;
}
